package com.dailyblue.java;

import lombok.SneakyThrows;

import java.io.*;

// 序列化和反序列化操作
public class DemoH {

    // 第一种序列化操作
    @SneakyThrows
    private static void a1() {
        Person person1 = new Person(1, "张三", '男', "2020-1-1", "陕西西安");
        Person person2 = new Person(2, "李四", '女', "2021-6-1", "陕西西安");
        // 要把对象保存到硬盘中
        File file = new File("/Users/dailyblue/a/person.obj");
        OutputStream out = new FileOutputStream(file);
        ObjectOutputStream oos = new ObjectOutputStream(out);
        oos.writeObject(person1);
        oos.writeObject(person2);
        oos.close();
        out.close();
    }

    // 第一种反序列化操作
    @SneakyThrows
    private static void a2() {
        File file = new File("/Users/dailyblue/a/person.obj");
        InputStream in = new FileInputStream(file);
        ObjectInputStream ois = new ObjectInputStream(in);
        Object obj1 = ois.readObject();
        Object obj2 = ois.readObject();
        System.out.println(obj1);
        System.out.println(obj2);
        ois.close();
        in.close();
    }

    @SneakyThrows
    private static void a3() {
        Person person1 = new Person(1, "张三", '男', "2020-1-1", "陕西西安");
        // 要把对象保存到硬盘中
        File file = new File("/Users/dailyblue/a/person.obj");
        OutputStream out = new FileOutputStream(file);
        ObjectOutputStream oos = new ObjectOutputStream(out);
        oos.writeObject(person1);
        oos.close();
        out.close();
        InputStream in = new FileInputStream(file);
        ObjectInputStream ois = new ObjectInputStream(in);
        Object obj1 = ois.readObject();
        System.out.println(obj1);
        ois.close();
        in.close();
        System.out.println(person1 == obj1);
    }

    // 第二种序列化方式
    @SneakyThrows
    private static void b1(){
        File file = new File("/Users/dailyblue/a/person.obj");
        OutputStream out = new FileOutputStream(file);
        Student student = new Student(1,"张三");
        student.writeObject(out);
    }
    // 第二种fan序列化方式
    @SneakyThrows
    private static void b2(){
        File file = new File("/Users/dailyblue/a/person.obj");
        InputStream in = new FileInputStream(file);
        Student student = new Student();
        Object obj = student.readObject(in);
        System.out.println(obj);
    }

    // 第三种序列化方式
    @SneakyThrows
    private static void c1(){
        File file = new File("/Users/dailyblue/a/person.obj");
        OutputStream out = new FileOutputStream(file);
        ObjectOutputStream oos = new ObjectOutputStream(out);
        School school = new School(1,"宝鸡文理学院");
        school.writeExternal(oos);
        oos.close();
        out.close();
    }
    // 第三种fan序列化方式
    @SneakyThrows
    private static void c2(){
        File file = new File("/Users/dailyblue/a/person.obj");
        InputStream in = new FileInputStream(file);
        ObjectInputStream ois = new ObjectInputStream(in);
        School school = new School();
        school.readExternal(ois);
        System.out.println(school);
    }
    public static void main(String[] args) {
        a1();
        a2();
    }

}
